using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
namespace Company.BLL.KDT.GC
{
	public partial class PhuKien
	{
		#region Private members.

        protected string _TenHang;
        protected string _MaHang;
        protected string _MaHS;
        protected long _ID;
		protected string _ThongTinCu = string.Empty;
        protected decimal _SoLuong;
        protected decimal _DonGia;
        protected string _DVT_ID;
        protected string _NuocXX_ID;
        protected decimal _trigia;
        protected string _NguyenTe_ID;
        protected int _STTHang;
        protected string _tinhtrang;
		protected long _Master_ID;
        protected string _NhomSP;
		#endregion
		
		//---------------------------------------------------------------------------------------------

		#region Properties.
		
		public long ID
		{
			set {this._ID = value;}
			get {return this._ID;}
		}
				
		
		public string MaHang
		{
            set { this._MaHang = value; }
            get { return this._MaHang; }
		}

        public string NhomSP
        {
            set { this._NhomSP = value; }
            get { return this._NhomSP; }
        }
		public string MaHS
		{
			set {this._MaHS = value;}
            get { return this._MaHS; }
		}
		
		public string ThongTinCu
		{
			set {this._ThongTinCu = value;}
            get { return this._ThongTinCu; }
		}
		
		public string TenHang
		{
            set { this._TenHang = value; }
            get { return this._TenHang; }
		}
		
		public decimal SoLuong
		{
			set {this._SoLuong = value;}
            get { return this._SoLuong; }
		}
		
		public int STTHang
		{
			set {this._STTHang = value;}
			get {return this._STTHang;}
		}

        public decimal DonGia
        {
            set { this._DonGia = value; }
            get { return this._DonGia; }
        }
		public long Master_ID
		{
			set {this._Master_ID = value;}
			get {return this._Master_ID;}
		}

        public string NuocXX_ID
        {
            set { this._NuocXX_ID = value; }
            get { return this._NuocXX_ID; }
        }
        public string DVT_ID
        {
            set { this._DVT_ID = value; }
            get { return this._DVT_ID; }
        }

        public decimal TriGia
        {
            set { this._trigia = value; }
            get { return this._trigia; }
        }

        public string NguyenTe
        {
            set { this._NguyenTe_ID = value; }
            get { return this._NguyenTe_ID; }
        }

        public string TinhTrang
        {
            set { this._tinhtrang = value; }
            get { return this._tinhtrang; }
        }
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Select methods.
		
		public static PhuKien Load(long iD)
		{
			string spName = "[dbo].p_KDT_GC_PhuKien_Load";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ID", SqlDbType.BigInt, iD);
			PhuKien entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
			if (reader.Read())
			{
				entity = new PhuKien();
				if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt64(reader.GetOrdinal("ID"));
				if (!reader.IsDBNull(reader.GetOrdinal("MaHang"))) entity.MaHang = reader.GetString(reader.GetOrdinal("MaHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("TenHang"))) entity.TenHang = reader.GetString(reader.GetOrdinal("TenHang"));
				if (!reader.IsDBNull(reader.GetOrdinal("MaHs"))) entity.MaHS = reader.GetString(reader.GetOrdinal("MaHs"));
				if (!reader.IsDBNull(reader.GetOrdinal("ThongTinCu"))) entity.ThongTinCu = reader.GetString(reader.GetOrdinal("ThongTinCu"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoLuong"))) entity.SoLuong = reader.GetDecimal(reader.GetOrdinal("SoLuong"));
                if (!reader.IsDBNull(reader.GetOrdinal("DonGia"))) entity.DonGia = reader.GetDecimal(reader.GetOrdinal("DonGia"));
				if (!reader.IsDBNull(reader.GetOrdinal("DVT_ID"))) entity.DVT_ID = reader.GetString(reader.GetOrdinal("DVT_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("NuocXX_ID"))) entity.NuocXX_ID = reader.GetString(reader.GetOrdinal("NuocXX_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("TriGia"))) entity.TriGia = reader.GetDecimal(reader.GetOrdinal("TriGia"));
                if (!reader.IsDBNull(reader.GetOrdinal("NguyenTe_ID"))) entity.NguyenTe = reader.GetString(reader.GetOrdinal("NguyenTe_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("STTHang"))) entity.STTHang = reader.GetInt32(reader.GetOrdinal("STTHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("TinhTrang"))) entity.TinhTrang = reader.GetString(reader.GetOrdinal("TinhTrang"));
                if (!reader.IsDBNull(reader.GetOrdinal("Master_ID"))) entity.Master_ID = reader.GetInt32(reader.GetOrdinal("Master_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("NhomSP"))) entity.NhomSP = reader.GetString(reader.GetOrdinal("NhomSP"));
				
			}
			reader.Close();
			dbCommand.Connection.Close();
			
			return entity;
		}

        public PhuKien LoadSanPham(long idhopdong, string mapk,string mahang)
        {
            string sql = "select * from t_KDT_GC_PhuKien pk " +
                        "inner join t_KDT_GC_PhuKienDangKy pkdk on pkdk.ID=pk.Master_ID " +
                        "where  pkdk.TrangThaiXuLy=1 and pkdk.HopDong_ID=@HopDong_ID and MaPhuKien=@MaPhuKien and pk.mahang=@mahang";         
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetSqlStringCommand(sql);
			db.AddInParameter(dbCommand, "@HopDong_ID", SqlDbType.BigInt, idhopdong);
            db.AddInParameter(dbCommand, "@MaPhuKien", SqlDbType.Char, mapk);
            db.AddInParameter(dbCommand, "@mahang", SqlDbType.VarChar, mahang);
          			
			PhuKien entity = null;
           
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                entity = new PhuKien();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt64(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHang"))) entity.MaHang = reader.GetString(reader.GetOrdinal("MaHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("TenHang"))) entity.TenHang = reader.GetString(reader.GetOrdinal("TenHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHs"))) entity.MaHS = reader.GetString(reader.GetOrdinal("MaHs"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinCu"))) entity.ThongTinCu = reader.GetString(reader.GetOrdinal("ThongTinCu"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoLuong"))) entity.SoLuong = reader.GetDecimal(reader.GetOrdinal("SoLuong"));
                if (!reader.IsDBNull(reader.GetOrdinal("DonGia"))) entity.DonGia = reader.GetDecimal(reader.GetOrdinal("DonGia"));
                if (!reader.IsDBNull(reader.GetOrdinal("DVT_ID"))) entity.DVT_ID = reader.GetString(reader.GetOrdinal("DVT_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("NuocXX_ID"))) entity.NuocXX_ID = reader.GetString(reader.GetOrdinal("NuocXX_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("TriGia"))) entity.TriGia = reader.GetDecimal(reader.GetOrdinal("TriGia"));
                if (!reader.IsDBNull(reader.GetOrdinal("NguyenTe_ID"))) entity.NguyenTe = reader.GetString(reader.GetOrdinal("NguyenTe_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("STTHang"))) entity.STTHang = reader.GetInt32(reader.GetOrdinal("STTHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("TinhTrang"))) entity.TinhTrang = reader.GetString(reader.GetOrdinal("TinhTrang"));
                if (!reader.IsDBNull(reader.GetOrdinal("Master_ID"))) entity.Master_ID = reader.GetInt64(reader.GetOrdinal("Master_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("NhomSP"))) entity.NhomSP = reader.GetString(reader.GetOrdinal("NhomSP"));
                reader.Close();
                return entity;
            }
            reader.Close();
            return null;
        }

        public PhuKien LoadSanPham(long idhopdong, string mapk, string mahang,string sopk)
        {
            string sql = "select * from t_KDT_GC_PhuKien pk " +
                        "inner join t_KDT_GC_PhuKienDangKy pkdk on pkdk.ID=pk.Master_ID " +
                        "where  pkdk.sophukien <> @SoPhuKien and pkdk.HopDong_ID=@HopDong_ID and MaPhuKien=@MaPhuKien and pk.mahang=@mahang";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetSqlStringCommand(sql);
            db.AddInParameter(dbCommand, "@HopDong_ID", SqlDbType.BigInt, idhopdong);
            db.AddInParameter(dbCommand, "@MaPhuKien", SqlDbType.Char, mapk);
            db.AddInParameter(dbCommand, "@mahang", SqlDbType.VarChar, mahang);
            db.AddInParameter(dbCommand, "@SoPhuKien", SqlDbType.VarChar, sopk);

            PhuKien entity = null;

            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                entity = new PhuKien();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt64(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHang"))) entity.MaHang = reader.GetString(reader.GetOrdinal("MaHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("TenHang"))) entity.TenHang = reader.GetString(reader.GetOrdinal("TenHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHs"))) entity.MaHS = reader.GetString(reader.GetOrdinal("MaHs"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinCu"))) entity.ThongTinCu = reader.GetString(reader.GetOrdinal("ThongTinCu"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoLuong"))) entity.SoLuong = reader.GetDecimal(reader.GetOrdinal("SoLuong"));
                if (!reader.IsDBNull(reader.GetOrdinal("DonGia"))) entity.DonGia = reader.GetDecimal(reader.GetOrdinal("DonGia"));
                if (!reader.IsDBNull(reader.GetOrdinal("DVT_ID"))) entity.DVT_ID = reader.GetString(reader.GetOrdinal("DVT_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("NuocXX_ID"))) entity.NuocXX_ID = reader.GetString(reader.GetOrdinal("NuocXX_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("TriGia"))) entity.TriGia = reader.GetDecimal(reader.GetOrdinal("TriGia"));
                if (!reader.IsDBNull(reader.GetOrdinal("NguyenTe_ID"))) entity.NguyenTe = reader.GetString(reader.GetOrdinal("NguyenTe_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("STTHang"))) entity.STTHang = reader.GetInt32(reader.GetOrdinal("STTHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("TinhTrang"))) entity.TinhTrang = reader.GetString(reader.GetOrdinal("TinhTrang"));
                if (!reader.IsDBNull(reader.GetOrdinal("Master_ID"))) entity.Master_ID = reader.GetInt64(reader.GetOrdinal("Master_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("NhomSP"))) entity.NhomSP = reader.GetString(reader.GetOrdinal("NhomSP"));
                reader.Close();
                return entity;
            }
            reader.Close();
            return null;
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectBy_Master_ID(long master_ID)
		{
			string spName = "[dbo].p_KDT_GC_PhuKien_SelectBy_Master_ID";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@Master_ID", SqlDbType.BigInt, master_ID);
						
            return db.ExecuteDataSet(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		

		//---------------------------------------------------------------------------------------------
		
		public static IDataReader SelectReaderBy_Master_ID(long master_ID)
		{
			string spName = "[dbo].p_KDT_GC_PhuKien_SelectBy_Master_ID";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@Master_ID", SqlDbType.BigInt, master_ID);
						
            return db.ExecuteReader(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------

        public static PhuKienCollection SelectCollectionTrangThaiDuyetById_HopDong(long idHopdong,string maphukien)
        {
            string spName = "select pk.*"+
                            "from  "+
                            "t_KDT_GC_PhuKienDangKy pkdk inner join t_KDT_GC_PhuKien pk on pkdk.ID=pk.Master_ID "+
                            "where pkdk.TrangThaiXuLy=1 and pkdk.MaPhuKien=@MaPhuKien and pkdk.HopDong_ID=@HopDong_ID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetSqlStringCommand(spName);

            db.AddInParameter(dbCommand, "@HopDong_ID", SqlDbType.BigInt, idHopdong);
            db.AddInParameter(dbCommand, "@MaPhuKien", SqlDbType.VarChar, maphukien);

            IDataReader reader = db.ExecuteReader(dbCommand);
            PhuKienCollection collection = new PhuKienCollection();
            while (reader.Read())
            {
                PhuKien entity = new PhuKien();

                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt64(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHang"))) entity.MaHang = reader.GetString(reader.GetOrdinal("MaHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("TenHang"))) entity.TenHang = reader.GetString(reader.GetOrdinal("TenHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHs"))) entity.MaHS = reader.GetString(reader.GetOrdinal("MaHs"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinCu"))) entity.ThongTinCu = reader.GetString(reader.GetOrdinal("ThongTinCu"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoLuong"))) entity.SoLuong = reader.GetDecimal(reader.GetOrdinal("SoLuong"));
                if (!reader.IsDBNull(reader.GetOrdinal("DonGia"))) entity.DonGia = reader.GetDecimal(reader.GetOrdinal("DonGia"));
                if (!reader.IsDBNull(reader.GetOrdinal("DVT_ID"))) entity.DVT_ID = reader.GetString(reader.GetOrdinal("DVT_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("NuocXX_ID"))) entity.NuocXX_ID = reader.GetString(reader.GetOrdinal("NuocXX_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("TriGia"))) entity.TriGia = reader.GetDecimal(reader.GetOrdinal("TriGia"));
                if (!reader.IsDBNull(reader.GetOrdinal("NguyenTe_ID"))) entity.NguyenTe = reader.GetString(reader.GetOrdinal("NguyenTe_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("STTHang"))) entity.STTHang = reader.GetInt32(reader.GetOrdinal("STTHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("TinhTrang"))) entity.TinhTrang = reader.GetString(reader.GetOrdinal("TinhTrang"));
                if (!reader.IsDBNull(reader.GetOrdinal("Master_ID"))) entity.Master_ID = reader.GetInt64(reader.GetOrdinal("Master_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("NhomSP"))) entity.NhomSP = reader.GetString(reader.GetOrdinal("NhomSP"));
                collection.Add(entity);
            }
            reader.Close();
            dbCommand.Connection.Close();
            return collection;

        }

        public static PhuKienCollection SelectCollectionTrangThaiDuyetById_HopDong(long idHopdong, string maphukien,string sophukien)
        {
            string spName = "select pk.*" +
                            "from  " +
                            "t_KDT_GC_PhuKienDangKy pkdk inner join t_KDT_GC_PhuKien pk on pkdk.ID=pk.Master_ID " +
                            "where pkdk.TrangThaiXuLy=1 and pkdk.MaPhuKien=@MaPhuKien and pkdk.HopDong_ID=@HopDong_ID  and pkdk.sophukien=@sophukien";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetSqlStringCommand(spName);

            db.AddInParameter(dbCommand, "@HopDong_ID", SqlDbType.BigInt, idHopdong);
            db.AddInParameter(dbCommand, "@MaPhuKien", SqlDbType.VarChar, maphukien);
            db.AddInParameter(dbCommand, "@sophukien", SqlDbType.VarChar, sophukien);
            IDataReader reader = db.ExecuteReader(dbCommand);
            PhuKienCollection collection = new PhuKienCollection();
            while (reader.Read())
            {
                PhuKien entity = new PhuKien();

                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt64(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHang"))) entity.MaHang = reader.GetString(reader.GetOrdinal("MaHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("TenHang"))) entity.TenHang = reader.GetString(reader.GetOrdinal("TenHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHs"))) entity.MaHS = reader.GetString(reader.GetOrdinal("MaHs"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinCu"))) entity.ThongTinCu = reader.GetString(reader.GetOrdinal("ThongTinCu"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoLuong"))) entity.SoLuong = reader.GetDecimal(reader.GetOrdinal("SoLuong"));
                if (!reader.IsDBNull(reader.GetOrdinal("DonGia"))) entity.DonGia = reader.GetDecimal(reader.GetOrdinal("DonGia"));
                if (!reader.IsDBNull(reader.GetOrdinal("DVT_ID"))) entity.DVT_ID = reader.GetString(reader.GetOrdinal("DVT_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("NuocXX_ID"))) entity.NuocXX_ID = reader.GetString(reader.GetOrdinal("NuocXX_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("TriGia"))) entity.TriGia = reader.GetDecimal(reader.GetOrdinal("TriGia"));
                if (!reader.IsDBNull(reader.GetOrdinal("NguyenTe_ID"))) entity.NguyenTe = reader.GetString(reader.GetOrdinal("NguyenTe_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("STTHang"))) entity.STTHang = reader.GetInt32(reader.GetOrdinal("STTHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("TinhTrang"))) entity.TinhTrang = reader.GetString(reader.GetOrdinal("TinhTrang"));
                if (!reader.IsDBNull(reader.GetOrdinal("Master_ID"))) entity.Master_ID = reader.GetInt64(reader.GetOrdinal("Master_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("NhomSP"))) entity.NhomSP = reader.GetString(reader.GetOrdinal("NhomSP"));
                collection.Add(entity);
            }
            reader.Close();
            dbCommand.Connection.Close();
            return collection;

        }
	

		public static PhuKienCollection SelectCollectionBy_Master_ID(long master_ID)
		{
			string spName = "[dbo].p_KDT_GC_PhuKien_SelectBy_Master_ID";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@Master_ID", SqlDbType.BigInt, master_ID);
						
            IDataReader reader = db.ExecuteReader(dbCommand);
			PhuKienCollection collection = new PhuKienCollection();			
			while (reader.Read())
			{
				PhuKien entity = new PhuKien();

                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt64(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHang"))) entity.MaHang = reader.GetString(reader.GetOrdinal("MaHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("TenHang"))) entity.TenHang = reader.GetString(reader.GetOrdinal("TenHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHs"))) entity.MaHS = reader.GetString(reader.GetOrdinal("MaHs"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinCu"))) entity.ThongTinCu = reader.GetString(reader.GetOrdinal("ThongTinCu"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoLuong"))) entity.SoLuong = reader.GetDecimal(reader.GetOrdinal("SoLuong"));
                if (!reader.IsDBNull(reader.GetOrdinal("DonGia"))) entity.DonGia = reader.GetDecimal(reader.GetOrdinal("DonGia"));
                if (!reader.IsDBNull(reader.GetOrdinal("DVT_ID"))) entity.DVT_ID = reader.GetString(reader.GetOrdinal("DVT_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("NuocXX_ID"))) entity.NuocXX_ID = reader.GetString(reader.GetOrdinal("NuocXX_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("TriGia"))) entity.TriGia = reader.GetDecimal(reader.GetOrdinal("TriGia"));
                if (!reader.IsDBNull(reader.GetOrdinal("NguyenTe_ID"))) entity.NguyenTe = reader.GetString(reader.GetOrdinal("NguyenTe_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("STTHang"))) entity.STTHang = reader.GetInt32(reader.GetOrdinal("STTHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("TinhTrang"))) entity.TinhTrang = reader.GetString(reader.GetOrdinal("TinhTrang"));
                if (!reader.IsDBNull(reader.GetOrdinal("Master_ID"))) entity.Master_ID = reader.GetInt64(reader.GetOrdinal("Master_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("NhomSP"))) entity.NhomSP = reader.GetString(reader.GetOrdinal("NhomSP"));
                collection.Add(entity);
			}
			reader.Close();
			dbCommand.Connection.Close();
			return collection;			
			
		}
		
		//---------------------------------------------------------------------------------------------
		

		public static DataSet SelectAll()
        {
            string spName = "[dbo].p_KDT_GC_PhuKien_SelectAll";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			
            return db.ExecuteDataSet(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------

		public static IDataReader SelectReaderAll()
        {
            string spName = "[dbo].p_KDT_GC_PhuKien_SelectAll";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			
            return db.ExecuteReader(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static PhuKienCollection SelectCollectionAll()
		{
			PhuKienCollection collection = new PhuKienCollection();

			IDataReader reader = SelectReaderAll();
			while (reader.Read())
			{
				PhuKien entity = new PhuKien();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt64(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHang"))) entity.MaHang = reader.GetString(reader.GetOrdinal("MaHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("TenHang"))) entity.TenHang = reader.GetString(reader.GetOrdinal("TenHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHs"))) entity.MaHS = reader.GetString(reader.GetOrdinal("MaHs"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinCu"))) entity.ThongTinCu = reader.GetString(reader.GetOrdinal("ThongTinCu"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoLuong"))) entity.SoLuong = reader.GetDecimal(reader.GetOrdinal("SoLuong"));
                if (!reader.IsDBNull(reader.GetOrdinal("DonGia"))) entity.DonGia = reader.GetDecimal(reader.GetOrdinal("DonGia"));
                if (!reader.IsDBNull(reader.GetOrdinal("DVT_ID"))) entity.DVT_ID = reader.GetString(reader.GetOrdinal("DVT_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("NuocXX_ID"))) entity.NuocXX_ID = reader.GetString(reader.GetOrdinal("NuocXX_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("TriGia"))) entity.TriGia = reader.GetDecimal(reader.GetOrdinal("TriGia"));
                if (!reader.IsDBNull(reader.GetOrdinal("NguyenTe_ID"))) entity.NguyenTe = reader.GetString(reader.GetOrdinal("NguyenTe_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("STTHang"))) entity.STTHang = reader.GetInt32(reader.GetOrdinal("STTHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("TinhTrang"))) entity.TinhTrang = reader.GetString(reader.GetOrdinal("TinhTrang"));
                if (!reader.IsDBNull(reader.GetOrdinal("Master_ID"))) entity.Master_ID = reader.GetInt32(reader.GetOrdinal("Master_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("NhomSP"))) entity.NhomSP = reader.GetString(reader.GetOrdinal("NhomSP"));
                collection.Add(entity);
			}
			reader.Close();
			return collection;			
		}
		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
		{
            string spName = "[dbo].p_KDT_GC_PhuKien_SelectDynamic";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteDataSet(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------

		public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
		{
            string spName = "[dbo].p_KDT_GC_PhuKien_SelectDynamic";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteReader(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
				
		public static PhuKienCollection SelectCollectionDynamic(string whereCondition, string orderByExpression)
		{
			PhuKienCollection collection = new PhuKienCollection();

			IDataReader reader = SelectReaderDynamic(whereCondition, orderByExpression);
			while (reader.Read())
			{
				PhuKien entity = new PhuKien();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt64(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHang"))) entity.MaHang = reader.GetString(reader.GetOrdinal("MaHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("TenHang"))) entity.TenHang = reader.GetString(reader.GetOrdinal("TenHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHs"))) entity.MaHS = reader.GetString(reader.GetOrdinal("MaHs"));
                if (!reader.IsDBNull(reader.GetOrdinal("ThongTinCu"))) entity.ThongTinCu = reader.GetString(reader.GetOrdinal("ThongTinCu"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoLuong"))) entity.SoLuong = reader.GetDecimal(reader.GetOrdinal("SoLuong"));
                if (!reader.IsDBNull(reader.GetOrdinal("DonGia"))) entity.DonGia = reader.GetDecimal(reader.GetOrdinal("DonGia"));
                if (!reader.IsDBNull(reader.GetOrdinal("DVT_ID"))) entity.DVT_ID = reader.GetString(reader.GetOrdinal("DVT_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("NuocXX_ID"))) entity.NuocXX_ID = reader.GetString(reader.GetOrdinal("NuocXX_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("TriGia"))) entity.TriGia = reader.GetDecimal(reader.GetOrdinal("TriGia"));
                if (!reader.IsDBNull(reader.GetOrdinal("NguyenTe_ID"))) entity.NguyenTe = reader.GetString(reader.GetOrdinal("NguyenTe_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("STTHang"))) entity.STTHang = reader.GetInt32(reader.GetOrdinal("STTHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("TinhTrang"))) entity.TinhTrang = reader.GetString(reader.GetOrdinal("TinhTrang"));
                if (!reader.IsDBNull(reader.GetOrdinal("Master_ID"))) entity.Master_ID = reader.GetInt32(reader.GetOrdinal("Master_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("NhomSP"))) entity.NhomSP = reader.GetString(reader.GetOrdinal("NhomSP"));
                collection.Add(entity);
			}
			reader.Close();
			return collection;			
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert methods.
		
		public static long Insert(string mahang, string tenhang, string mahs,string thongtincu, decimal soluong,decimal dongia,string DVT,string nguyente,string nuocXX,decimal trigia,int STTHang,string tinhtrang,int Master_ID,string nhomSP)
		{
			PhuKien entity = new PhuKien();
            entity.MaHang =  mahang;
			entity.TenHang = tenhang;
			entity.MaHS = mahs;
			entity.ThongTinCu = thongtincu;			
			entity.SoLuong = soluong;
			entity.DonGia = dongia;
			entity.STTHang = STTHang;
			entity.Master_ID = Master_ID;
            entity.DVT_ID = DVT;
            entity.NguyenTe = nguyente;
            entity.NuocXX_ID = nuocXX;
            entity.TriGia = trigia;
            entity.TinhTrang = tinhtrang;
            entity.NhomSP = nhomSP;
			long returnID = entity.Insert();
						
			return returnID;
		}
		
		public bool Insert(PhuKienCollection collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection) db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
					bool ret01 = true;
                    foreach (PhuKien item in collection)
                    {
                        if (item.InsertTransaction(transaction) <= 0)
						{							
							ret01 = false;
							break;
						}
                    }
					if (ret01)
					{
                    	transaction.Commit();
                    	ret = true;
					}
					else
					{
                    	transaction.Rollback();
						ret = false;                    	
					}
                }
                catch
                {
                    ret = false;
                    transaction.Rollback();
                }
                finally 
                {
                    connection.Close();
                }
            }
            return ret;		
		}
		
		public long Insert()
		{
			return this.InsertTransaction(null);
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public long InsertTransaction(SqlTransaction transaction)
		{			
			string spName = "[dbo].p_KDT_GC_PhuKien_Insert";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddOutParameter(dbCommand, "@ID", SqlDbType.BigInt, 8);
			db.AddInParameter(dbCommand, "@Mahang", SqlDbType.VarChar, this._MaHang);
			db.AddInParameter(dbCommand, "@TenHang", SqlDbType.NVarChar, this._TenHang);
			db.AddInParameter(dbCommand, "@MaHs", SqlDbType.VarChar, this._MaHS);
			db.AddInParameter(dbCommand, "@ThongTinCu", SqlDbType.NVarChar, this._ThongTinCu);
			db.AddInParameter(dbCommand, "@SoLuong", SqlDbType.Decimal, this._SoLuong);
			db.AddInParameter(dbCommand, "@DonGia", SqlDbType.Money, this._DonGia);
			db.AddInParameter(dbCommand, "@DVT_ID", SqlDbType.VarChar, this.DVT_ID);
			db.AddInParameter(dbCommand, "@NuocXX_ID", SqlDbType.VarChar, this.NuocXX_ID);
			db.AddInParameter(dbCommand, "@TriGia", SqlDbType.Money, this._trigia);
            db.AddInParameter(dbCommand, "@NguyenTe_ID", SqlDbType.VarChar, this._NguyenTe_ID);
            db.AddInParameter(dbCommand, "@STTHang", SqlDbType.Int, this._STTHang);
            db.AddInParameter(dbCommand, "@Master_ID", SqlDbType.BigInt, this._Master_ID);
            db.AddInParameter(dbCommand, "@NhomSP", SqlDbType.VarChar, this._NhomSP);
            db.AddInParameter(dbCommand, "@TinhTrang", SqlDbType.NVarChar, this.TinhTrang);
			if (transaction != null)
			{
				db.ExecuteNonQuery(dbCommand, transaction);
				this._ID = (long) db.GetParameterValue(dbCommand, "@ID");
				return this._ID;
			}
            else
			{
				db.ExecuteNonQuery(dbCommand);
				this._ID = (long) db.GetParameterValue(dbCommand, "@ID");
				return this._ID;
			}			
		}
		
		//---------------------------------------------------------------------------------------------
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert / Update methods.
		public int InsertUpdate()
		{
			return this.InsertUpdateTransaction(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int InsertUpdateTransaction(SqlTransaction transaction)
		{			
			string spName = "p_KDT_GC_PhuKien_InsertUpdate";	
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			db.AddInParameter(dbCommand, "@ID", SqlDbType.BigInt, this._ID);
            db.AddInParameter(dbCommand, "@Mahang", SqlDbType.VarChar, this._MaHang);
            db.AddInParameter(dbCommand, "@TenHang", SqlDbType.NVarChar, this._TenHang);
            db.AddInParameter(dbCommand, "@MaHs", SqlDbType.VarChar, this._MaHS);
            db.AddInParameter(dbCommand, "@ThongTinCu", SqlDbType.NVarChar, this._ThongTinCu);
            db.AddInParameter(dbCommand, "@SoLuong", SqlDbType.Decimal, this._SoLuong);
            db.AddInParameter(dbCommand, "@DonGia", SqlDbType.Money, this._DonGia);
            db.AddInParameter(dbCommand, "@DVT_ID", SqlDbType.VarChar, this.DVT_ID);
            db.AddInParameter(dbCommand, "@NuocXX_ID", SqlDbType.VarChar, this.NuocXX_ID);
            db.AddInParameter(dbCommand, "@TriGia", SqlDbType.Money, this._trigia);
            db.AddInParameter(dbCommand, "@NguyenTe_ID", SqlDbType.VarChar, this._NguyenTe_ID);
            db.AddInParameter(dbCommand, "@STTHang", SqlDbType.Int, this._STTHang);
            db.AddInParameter(dbCommand, "@TinhTrang", SqlDbType.NVarChar, this.TinhTrang);
            db.AddInParameter(dbCommand, "@Master_ID", SqlDbType.BigInt, this._Master_ID);
            db.AddOutParameter(dbCommand, "@IDOut", SqlDbType.BigInt, 8);
            db.AddInParameter(dbCommand, "@NhomSP", SqlDbType.VarChar, this._NhomSP);
            int n = 0;
            if (transaction != null)
            {
                n = db.ExecuteNonQuery(dbCommand, transaction);

            }
            else
            {
                n = db.ExecuteNonQuery(dbCommand);
            }
            this._ID = (long)db.GetParameterValue(dbCommand, "@IDOut");
            return n;
		}
		
		//---------------------------------------------------------------------------------------------
		
		public bool InsertUpdate(PhuKienCollection collection)
        {
            bool ret;			
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection) db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
					bool ret01 = true;
                    foreach (PhuKien item in collection)
                    {
                        if (item.InsertUpdateTransaction(transaction) <= 0)
						{
							ret01 = false;
							break;
						}
                    }
					if (ret01)
					{
						transaction.Commit();
						ret = true;
					}
					else
					{
						transaction.Rollback();
						ret = false;                    	
					}
                }
                catch
                {
                    ret = false;
                    transaction.Rollback();
                }
                finally 
                {
                    connection.Close();
                }
            }
            return ret;		
		}		
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Update methods.

        public static int Update(long id,string mahang, string tenhang, string mahs, string thongtincu, decimal soluong, decimal dongia, string DVT, string nguyente, string nuocXX, decimal trigia, int STTHang, string tinhtrang, int Master_ID,string nhomsp)
		{
			PhuKien entity = new PhuKien();
            entity.MaHang = mahang;
            entity.TenHang = tenhang;
            entity.MaHS = mahs;
            entity.ThongTinCu = thongtincu;
            entity.SoLuong = soluong;
            entity.DonGia = dongia;
            entity.STTHang = STTHang;
            entity.Master_ID = Master_ID;
            entity.DVT_ID = DVT;
            entity.NguyenTe = nguyente;
            entity.NuocXX_ID = nuocXX;
            entity.TriGia = trigia;
            entity.TinhTrang = tinhtrang;
            entity.ID = id;
            entity._NhomSP = nhomsp;
			return entity.Update();
		}
		
		public int Update()
		{
			return this.UpdateTransaction(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public bool Update(PhuKienCollection collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection) db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
					bool ret01 = true;
                    foreach (PhuKien item in collection)
                    {
                        if (item.UpdateTransaction(transaction) <= 0)
						{							
							ret01 = false;
							break;
						}
                    }
					if (ret01)
					{
                    	transaction.Commit();
                    	ret = true;
					}
					else
					{
                    	transaction.Rollback();
						ret = false;                    	
					}
                }
                catch
                {
                    ret = false;
                    transaction.Rollback();
                }
                finally 
                {
                    connection.Close();
                }
            }
            return ret;		
		}

		public int UpdateTransaction(SqlTransaction transaction)
		{
			string spName = "[dbo].p_KDT_GC_PhuKien_Update";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ID", SqlDbType.BigInt, this._ID);
            db.AddInParameter(dbCommand, "@Mahang", SqlDbType.VarChar, this._MaHang);
            db.AddInParameter(dbCommand, "@TenHang", SqlDbType.NVarChar, this._TenHang);
            db.AddInParameter(dbCommand, "@MaHs", SqlDbType.VarChar, this._MaHS);
            db.AddInParameter(dbCommand, "@ThongTinCu", SqlDbType.NVarChar, this._ThongTinCu);
            db.AddInParameter(dbCommand, "@SoLuong", SqlDbType.Decimal, this._SoLuong);
            db.AddInParameter(dbCommand, "@DonGia", SqlDbType.Money, this._DonGia);
            db.AddInParameter(dbCommand, "@DVT_ID", SqlDbType.VarChar, this.DVT_ID);
            db.AddInParameter(dbCommand, "@NuocXX_ID", SqlDbType.VarChar, this.NuocXX_ID);
            db.AddInParameter(dbCommand, "@TriGia", SqlDbType.Money, this._trigia);
            db.AddInParameter(dbCommand, "@NguyenTe_ID", SqlDbType.VarChar, this._NguyenTe_ID);
            db.AddInParameter(dbCommand, "@STTHang", SqlDbType.Int, this._STTHang);
            db.AddInParameter(dbCommand, "@Master_ID", SqlDbType.BigInt, this._Master_ID);
            db.AddInParameter(dbCommand, "@NhomSP", SqlDbType.VarChar, this._NhomSP);
            db.AddInParameter(dbCommand, "@TinhTrang", SqlDbType.NVarChar, this.TinhTrang);
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Delete methods.
		
		public static int Delete(long iD)
		{
			PhuKien entity = new PhuKien();
			entity.ID = iD;
			return entity.Delete();
		}
		
		public int Delete()
		{
			return this.DeleteTransaction(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public bool DeleteCollection(PhuKienCollection collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection) db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
					bool ret01 = true;
                    foreach (PhuKien item in collection)
                    {
                        if (item.DeleteTransaction(transaction) <= 0)
						{
							ret01 = false;
							break;
						}
                    }
					if (ret01)
					{
						transaction.Commit();
						ret = true;
					}
					else
					{
						transaction.Rollback();
						ret = false;                    	
					}
                }
                catch
                {
                    ret = false;
                    transaction.Rollback();
                }
                finally 
                {
                    connection.Close();
                }
            }
            return ret;		
		}

		//---------------------------------------------------------------------------------------------

		public static void DeleteCollection(PhuKienCollection collection, SqlTransaction transaction)
		{			
            foreach (PhuKien item in collection)
            {
                item.DeleteTransaction(transaction);
            }			
		}

		public int DeleteTransaction(SqlTransaction transaction)
		{
			string spName = "[dbo].p_KDT_GC_PhuKien_Delete";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ID", SqlDbType.BigInt, this._ID);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
				
		#endregion
	}	
}